﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using BluePortal.Medias.Core;
using BluePortal.Encoding.Core.Configuration;
using BluePortal.Encoding.Core.Interface;
using BluePortal.Framework;

namespace BluePortal.QuikStart.UserControls
{
    public partial class VideoEdit : System.Web.UI.UserControl
    {
        public Guid vid = Guid.Empty;
        public FormViewMode viewMode;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["vid"] != null)
            {
                vid = new Guid(Request.QueryString["vid"]);
            }
            else 
            {
                if (ViewState["vid"] != null)
                {
                    vid = (Guid)ViewState["vid"];
                }
            }
            if (vid != Guid.Empty)
            {
                //@todo
                //BCVideo bcvideo = new BCVideo();
                //Video v = bcvideo.FindVideoByID(vid);
                //if ((v!=null) && (v.Owner != Page.User.Identity.Name))
                //{
                //    Response.Redirect("Default.aspx");
                //}
            }

            if (!Page.IsPostBack)
            {
                ObjectDataSource1.SelectParameters["id"].DefaultValue = vid.ToString();
                FormView1.DefaultMode = viewMode;
                if (FormView1.FindControl("RadioButtonListStreamingProcess") != null)
                {
                    ((RadioButtonList)FormView1.FindControl("RadioButtonListStreamingProcess")).DataSource
                         = ((StreamServiceSection)ConfigurationManager.GetSection("streamServices")).StreamService;
                    ((RadioButtonList)FormView1.FindControl("RadioButtonListStreamingProcess")).DataBind();
                    ((RadioButtonList)FormView1.FindControl("RadioButtonListStreamingProcess")).SelectedIndex = 0;
                }
                ViewState["vid"] = vid;
            }
        }

        protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            BCVideo bcvideo = new BCVideo();
            if (e.CommandName == "UpdateVideo")
            {                
                Video v = bcvideo.FindVideoByID((Guid)ViewState["vid"]);
                if (v == null)
                {
                    v = new Video() { ID = (Guid)ViewState["vid"] };
                }
                v.Owner = Page.User.Identity.Name;
                v.Title = ((TextBox)FormView1.FindControl("tbTitle")).Text;
                v.Description = ((TextBox)FormView1.FindControl("tbDescription")).Text;
                Control fuThumbnail = FormView1.FindControl("fuThumbnail");

                if ((fuThumbnail != null) && (((FileUpload)fuThumbnail).FileName != String.Empty))
                    v.Preview = ((FileUpload)fuThumbnail).FileBytes;
                bcvideo.CreateOrUpdate(v);
                
                if (FormView1.FindControl("FinishButton") != null)
                {
                    FormView1.FindControl("FinishButton").Visible = true;
                }
            }
            if (e.CommandName == "DeleteVideo")
            { 
                Guid vid = (Guid)ViewState["vid"];
                if (vid != null)
                {
                    bcvideo.Delete(vid);
                    Response.Redirect("EditUser.aspx");
                }
                
            }
            if (e.CommandName == "Finish")
            {
                Video v = bcvideo.FindVideoByID((Guid)ViewState["vid"]);
                if (v != null)
                {
                    IVideoEncoderProcess srv = Factory.CreateWCFProxy<IVideoEncoderProcess>();
                  
                    srv.Encode(v.ID, ((StreamServiceSection)ConfigurationManager.GetSection("streamServices")).StreamService[((RadioButtonList)FormView1.FindControl("RadioButtonListStreamingProcess")).SelectedValue].StreamService
                                   , ((StreamServiceSection)ConfigurationManager.GetSection("streamServices")).StreamService[((RadioButtonList)FormView1.FindControl("RadioButtonListStreamingProcess")).SelectedValue].Parameters);

                    ViewState["vid"] = null;
                    Session["newvid"] = null;
                }
                Response.Redirect("EditUser.aspx");
            }

        }
    }
}